using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BloodBot
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Sounds : Microsoft.Xna.Framework.GameComponent
    {
        SoundEffect[] enemyDeath;
        SoundEffectInstance enemyDeathInstance;
        float enemyDeathVolume = 0.75f;

       public SoundEffect heartBeat;
       public SoundEffectInstance heartBeatInstance;
        float heartBeatVolume = 0.1f;

        SoundEffect laser;
        SoundEffectInstance laserInstance;
        float laserVolume = 0.5f;

        SoundEffect nextLevel;
        SoundEffectInstance nextLevelInstance;

        SoundEffect gameOver;
        SoundEffectInstance gameOverInstance;

        SoundEffect laserCollision;
        SoundEffectInstance laserCollisionInstance;

        SoundEffect enemyAttack;
        SoundEffectInstance enemyAttackInstance;

        AudioEmitter emitter = new AudioEmitter();
        AudioListener listener = new AudioListener();

        public Sounds(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here



            base.Initialize();
        }

        public void Load()
        {
            enemyDeath = new SoundEffect[4];

            enemyDeath[0] = Game.Content.Load<SoundEffect>(@"Sounds\squish3");
            enemyDeath[1] = Game.Content.Load<SoundEffect>(@"Sounds\squish4");
            enemyDeath[2] = Game.Content.Load<SoundEffect>(@"Sounds\squish6");

            heartBeat = Game.Content.Load<SoundEffect>(@"Sounds\heartBeat");
            laser = Game.Content.Load<SoundEffect>(@"Sounds\laser");
            nextLevel = Game.Content.Load<SoundEffect>(@"Sounds\nextLevel");
            gameOver = Game.Content.Load<SoundEffect>(@"Sounds\gameOver");
            laserCollision = Game.Content.Load<SoundEffect>(@"Sounds\laserEnemyCollision");
            enemyAttack = Game.Content.Load<SoundEffect>(@"Sounds\enemyAttack");

            heartBeatInstance = heartBeat.CreateInstance();
            heartBeatInstance.IsLooped = true;
            heartBeatInstance.Volume = heartBeatVolume;

            enemyAttackInstance = enemyAttack.CreateInstance();

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, Player player)
        {
            // TODO: Add your update code here

            float modifier = player.currentHealth / player.totalHealth;

            if (modifier > 0.9)
            {
                modifier = 0.9f;
            }

            if (heartBeatInstance.IsDisposed == false)
            {
                heartBeatVolume = 1 - modifier;
                heartBeatInstance.Volume = heartBeatVolume;

                heartBeatInstance.Play();
            }

            base.Update(gameTime);
        }

        public void PlayEnemyDeathSound(int chosenValue)
        {
            enemyDeathInstance = enemyDeath[chosenValue].CreateInstance();
            enemyDeathInstance.Volume = enemyDeathVolume;

            enemyDeathInstance.Play();
        }

        public void PlayLaserSound()
        {
            laserInstance = laser.CreateInstance();
            laserInstance.Volume = laserVolume;

            laserInstance.Play();
        }

        public void PlayNextLevelSound()
        {
            nextLevelInstance = nextLevel.CreateInstance();

            nextLevelInstance.Play();
        }

        public void PlayGameOverSound()
        {
            gameOverInstance = gameOver.CreateInstance();

            gameOverInstance.Play();
        }

        public void PlayLaserCollisionSound(Vector3 emitterPos, Vector3 listenerPos)
        {
            emitter.Position = emitterPos;
            listener.Position = listenerPos;

            laserCollisionInstance = laserCollision.CreateInstance();
            laserCollisionInstance.Apply3D(listener, emitter);

            laserCollisionInstance.Play();
        }

        public void PlayEnemyAttackSound()
        {
            if (enemyAttackInstance.State != SoundState.Playing)
            {
                enemyAttackInstance.Play();
            }
        }
    }
}
